﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Bookings : Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl
{
    protected override void OnInit(EventArgs e)
    {
         base.OnInit(e);
         dsBookingSlots.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsBookingSlots_Select);
         list.ItemDataBound += new EventHandler<ListViewItemEventArgs>(list_ItemDataBound);
         dsBookings.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsBookings_Select);
    }

    protected void download_FieldValueDataBound(object sender, Acms.WebControls.DownloadingGrid.FieldValueDataBoundEventArgs e)
    {
        if (e.Field.HeaderText == "Brand Union")
        {
            e.Value = ((BrandUnion.CustomData.Booking)e.DataItem).UserId.HasValue ? "true" : "false";
        }
        
    }

    void dsBookings_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(this.AllBookings);
    }

    void list_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            Acms.WebControls.SimpleRepeater rpt = e.Item.FindControl("rpt") as Acms.WebControls.SimpleRepeater;
            BrandUnion.CustomData.BookingSlot slot = ((ListViewDataItem)e.Item).DataItem as BrandUnion.CustomData.BookingSlot;
            if (rpt != null && slot != null)
            {
                rpt.DataSource = AllBookings.Where(x => x.BookingSlotId == slot.Id).ToList();
                rpt.DataBind();
            }
        }
    }

    void dsBookingSlots_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(new BrandUnion.CustomData.BookingSlotFactory().GetAll());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    private List<BrandUnion.CustomData.Booking> _AllBookings;
    protected List<BrandUnion.CustomData.Booking> AllBookings
    {
        get
        {
            if (_AllBookings == null)
            {
                _AllBookings = new BrandUnion.CustomData.BookingFactory().GetAll();
            }
            return _AllBookings;
        }
    }
}
